package org.stone.project.pro.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stone.project.pro.dao.TxMsgDao;
import org.stone.project.pro.domain.TxMsg;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

@Service
public class TxMsgService {
    @Autowired
    private TxMsgDao txMsgDao;

    /**
     * 简单的保存接口
     */
    public void test1(){
        TxMsg txMsg = new TxMsg();
        txMsg.setUsername("stone");
        txMsg.setPassword("pwd");
        txMsg.setHeadImg("headImg");

        txMsgDao.save(txMsg)
                .subscribeOn(Schedulers.single())
                .subscribe(e->{
                    System.out.println(Thread.currentThread().getName());
                    System.out.println("保存成功");
                });
    }

    /**
     * 事务测试
     */
    @Transactional
    public Mono<TxMsg> test2(){
        TxMsg txMsg = new TxMsg();
        txMsg.setUsername("stone1");
        txMsg.setPassword("pwd");
        txMsg.setHeadImg("headImg");

        return txMsgDao.save(txMsg)
                .map(e->{
                    if (1 == 1){
                        throw new IllegalArgumentException("参数异常");
                    }
                    return e;
                });
    }

}
